﻿using System;
using System.Xml;

namespace Devv.FreshTracker.Entity
{
	[Serializable]
	public class Task
	{
		#region Properties

		public Int64 Id { get; set; }
		public String Name { get; set; }
		public Project Project { get; set; }

		public String DisplayMember
		{
			get { return ToString(); }
		}

		public String FullDisplayMember
		{
			get { return Project.Client.DisplayMember + ": " + Project.Name + " - " + Name; }
		}

		#endregion

		#region Constructor

		public Task()
		{
			Project = new Project();
		}

		public Task(XmlNode node, Project project = null)
		{
			XmlNamespaceManager ns = new XmlNamespaceManager(node.OwnerDocument.NameTable);
			ns.AddNamespace("ns", FreshBooks.DEFAULT_XMLNS);

			Id = Convert.ToInt64(node.SelectSingleNode("ns:task_id", ns).InnerText);
			Name = node.SelectSingleNode("ns:name", ns).InnerText;

			if (project == null)
			{
				Project = new Project();

				XmlNode projectId = node.SelectSingleNode("ns:project_id", ns);

				if (projectId != null && !String.IsNullOrEmpty(projectId.InnerText))
				{
					Project.Id = Convert.ToInt64(projectId);
				}
			}
			else
			{
				Project = project;
			}
		}

		#endregion

		#region ToString

		public override String ToString()
		{
			return Name;
		}

		#endregion
	}
}